package cn.stronglink.asset.module.assetmanage.transfer.service;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetExtUsing;
import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.PullOff;
import cn.stronglink.asset.model.PullOffDetail;
import cn.stronglink.asset.model.PutOn;
import cn.stronglink.asset.model.PutOnDetail;
import cn.stronglink.asset.model.Transfer;
import cn.stronglink.asset.model.TransferDetail;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.pulloff.mapper.PullOffMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.service.PullOffService;
import cn.stronglink.asset.module.assetmanage.puton.mapper.PutOnMapper;
import cn.stronglink.asset.module.assetmanage.puton.service.PutOnService;
import cn.stronglink.asset.module.assetmanage.transfer.mapper.TransferMapper;
import cn.stronglink.asset.module.assetmanage.transfer.vo.OriginalAssetInfo;

import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class TransferService {

	@Autowired
	private TransferMapper transferMapper;

	@Autowired
	private PullOffMapper pullOffMapper;

	@Autowired
	private PutOnMapper putOnMapper;
	@Autowired
	private PutOnService putOnService;
	@Autowired
	private PullOffService pullOffService;

	@Autowired
	private AssetRecordMapper assetRecordMapper;
	/**
	 * 新增迁移单
	 */
	public void create(Transfer transfer) {
		// 迁移单
		if (transfer.getId() == null) {
			transfer.setId(IdWorker.getId());
		}
		transfer.setStatus(1);
		transfer.setCreateBy(WebUtil.getCurrentUser());
		transfer.setCreateTime(new Date());
		transferMapper.insertTransfer(transfer);

		if (transfer.getTransferDetailList() != null && transfer.getTransferDetailList().size() > 0) {
			Set<Long> originalRoomIdSet = new HashSet<Long>();
			Set<Long> targetRoomIdSet = new HashSet<Long>();
			for (TransferDetail detail : transfer.getTransferDetailList()) {
				OriginalAssetInfo assetInfo = transferMapper.getAsset(Long.valueOf(detail.getAssetId_()));
				if (assetInfo != null) {
					detail.setOriginalManageBranchId(assetInfo.getOriginalManageBranchId());
					detail.setOriginalUseBranchId(assetInfo.getOriginalUseBranchId());
					detail.setOriginalEndUser(assetInfo.getOriginalEndUser());
					detail.setOriginalRoomId(assetInfo.getOriginalRoomId());
					detail.setOriginalRackId(assetInfo.getOriginalRackId());
					detail.setOriginalStart(assetInfo.getOriginalStart());
					detail.setOriginalEnd(assetInfo.getOriginalEnd());
					originalRoomIdSet.add(assetInfo.getOriginalRoomId());
				}
				
				targetRoomIdSet.add(detail.getTargetRoomId());
			}

			// 创建下架单
			Map<Long, PullOff> pullOffMap = new HashMap<>();
			for (Long originalRoomId : originalRoomIdSet) {
				// 下架单
				PullOff pullOff = new PullOff();
				pullOff.setId(IdWorker.getId());
				pullOff.setExecuteTime(transfer.getExecuteTime());
				pullOff.setOperatorBy(transfer.getOperatorBy());
				pullOff.setRoomId(originalRoomId);
				pullOff.setType(2);
				pullOff.setStatus(1);
				pullOff.setRemark(transfer.getRemark());
				pullOff.setCreateTime(new Date());
				pullOff.setCreateBy(WebUtil.getCurrentUser());
				pullOffMap.put(originalRoomId, pullOff);
				pullOffMapper.insertPullOff(pullOff);
				for (TransferDetail detail : transfer.getTransferDetailList()) {
					if(detail.getOriginalRoomId()==originalRoomId) {
						// 下架单详情
						PullOffDetail pullOffDetail = new PullOffDetail();
						pullOffDetail.setId(IdWorker.getId());
						pullOffDetail.setPullOffId(pullOff.getId());
						pullOffDetail.setAssetId(Long.valueOf(detail.getAssetId_()));
						pullOffDetail.setStatus(2);
						pullOffDetail.setCreateTime(new Date());
						pullOffDetail.setCreateBy(WebUtil.getCurrentUser());
						pullOffMapper.insertPullOffDetail(pullOffDetail);
					}
				}
			}

			// 创建上架单
			Map<Long, PutOn> putOnMap = new HashMap<>();
			for (Long targetRoomId : targetRoomIdSet) {
				PutOn putOn = new PutOn();
				putOn.setId(IdWorker.getId());
				putOn.setExecuteTime(transfer.getExecuteTime());
				putOn.setOperatorBy(transfer.getOperatorBy());
				putOn.setRoomId(targetRoomId);
				putOn.setType(2);
				putOn.setStatus(1);
				putOn.setRemark(transfer.getRemark());
				putOn.setCreateTime(new Date());
				putOn.setCreateBy(WebUtil.getCurrentUser());
				putOnMap.put(targetRoomId, putOn);
				putOnMapper.insertPutOn(putOn);
				for (TransferDetail detail : transfer.getTransferDetailList()) {
					// 上架单详情
					if(detail.getTargetRoomId()==targetRoomId) {
						PutOnDetail putOnDetail = new PutOnDetail();
						putOnDetail.setId(IdWorker.getId());
						putOnDetail.setPutOnId(putOn.getId());
						putOnDetail.setAssetId(Long.valueOf(detail.getAssetId_()));
						putOnDetail.setRoomId(detail.getTargetRoomId().toString());
						putOnDetail.setRackId(detail.getTargetRackId().toString());
						putOnDetail.setStartU(detail.getTargetStart());
						putOnDetail.setStatus(2);
						putOnDetail.setCreateBy(WebUtil.getCurrentUser());
						putOnDetail.setCreateTime(new Date());
						putOnMapper.insertPutOnDetail(putOnDetail);
					}

				}
			}

			for (TransferDetail detail : transfer.getTransferDetailList()) {
				detail.setTransferId(transfer.getId());
				detail.setAssetId(Long.valueOf(detail.getAssetId_()));
				detail.setPullOffId(pullOffMap.get(detail.getOriginalRoomId()).getId());
				detail.setPutOnId(putOnMap.get(detail.getTargetRoomId()).getId());
				detail.setStatus(2);
				detail.setId(IdWorker.getId());
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				transferMapper.insertTransferDetail(detail);
			}
		}
	}

	public void remove(Long id) {
		List<TransferDetail> detailList = transferMapper.qryTransferDetailByTransferId(id);
		if (detailList != null && detailList.size() > 0) {
			// Long putOnId = detailList.get(0).getPutOnId();
			// Long pullOffId = detailList.get(0).getPullOffId();
			// 移除迁移单详情
			for (TransferDetail transferDetail : detailList) {
				transferMapper.delTransferDetail(transferDetail.getId());
				Long putOnId = transferDetail.getPutOnId();
				Long pullOffId = transferDetail.getPullOffId();
				putOnService.remove(putOnId);
				pullOffService.remove(pullOffId);
			}
		}
		// 移除迁移单
		transferMapper.remove(id);
	}

	public void edit(Transfer transfer) {
		transfer.setStatus(1);
		transfer.setUpdateBy(WebUtil.getCurrentUser());
		transfer.setUpdateTime(new Date());
		transferMapper.updateTranser(transfer);

		if (transfer.getTransferDetailList() != null && transfer.getTransferDetailList().size() > 0) {
			Long putOnId = transfer.getTransferDetailList().get(0).getPutOnId();
			Long pullOffId = transfer.getTransferDetailList().get(0).getPullOffId();

			putOnService.remove(putOnId);
			pullOffService.remove(pullOffId);

			for (TransferDetail transferDetail : transfer.getTransferDetailList()) {
				transferDetail.setStatus(2);
				transferDetail.setUpdateBy(WebUtil.getCurrentUser());
				transferDetail.setUpdateTime(new Date());
				transferMapper.updateTransferDetail(transferDetail);

				// 上架单详情
				PutOnDetail putOnDetail = new PutOnDetail();
				putOnDetail.setId(IdWorker.getId());
				putOnDetail.setPutOnId(putOnId);
				putOnDetail.setAssetId(Long.valueOf(transferDetail.getAssetId_()));
				putOnDetail.setRoomId(transferDetail.getTargetRoomId().toString());
				putOnDetail.setRackId(transferDetail.getTargetRackId().toString());
				putOnDetail.setStartU(transferDetail.getTargetStart());
				putOnDetail.setStatus(2);
				putOnDetail.setCreateBy(WebUtil.getCurrentUser());
				putOnDetail.setCreateTime(new Date());
				putOnMapper.insertPutOnDetail(putOnDetail);

				// 下架单详情
				PullOffDetail pullOffDetail = new PullOffDetail();
				pullOffDetail.setId(IdWorker.getId());
				pullOffDetail.setPullOffId(pullOffId);
				pullOffDetail.setAssetId(Long.valueOf(transferDetail.getAssetId_()));
				pullOffDetail.setStatus(2);
				pullOffDetail.setCreateTime(new Date());
				pullOffDetail.setCreateBy(WebUtil.getCurrentUser());
				pullOffMapper.insertPullOffDetail(pullOffDetail);
			}

			// 下架单
			PullOff pullOff = new PullOff();
			pullOff.setId(pullOffId);
			pullOff.setExecuteTime(transfer.getExecuteTime());
			pullOff.setOperatorBy(transfer.getOperatorBy());
			pullOff.setType(2);
			pullOff.setStatus(1);
			pullOff.setRemark(transfer.getRemark());
			pullOff.setCreateTime(new Date());
			pullOff.setCreateBy(WebUtil.getCurrentUser());
			pullOffMapper.insertPullOff(pullOff);

			// 上架单
			PutOn putOn = new PutOn();
			putOn.setId(putOnId);
			putOn.setExecuteTime(transfer.getExecuteTime());
			putOn.setOperatorBy(transfer.getOperatorBy());
			putOn.setType(2);
			putOn.setStatus(1);
			putOn.setRemark(transfer.getRemark());
			putOn.setCreateTime(new Date());
			putOn.setCreateBy(WebUtil.getCurrentUser());
			putOnMapper.insertPutOn(putOn);
		}
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Transfer> queryTransferList(Page<Transfer> page, Map<String, Object> params) {
		List<Transfer> transferList = transferMapper.queryTransferList(page, params);
		page.setRecords(transferList);
		return page;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<TransferDetail> queryTransferDetailList(Page<TransferDetail> page, Map<String, Object> params) {
		List<TransferDetail> transferDetailList = transferMapper.queryTransferDetailList(page, params);
		page.setRecords(transferDetailList);
		return page;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Transfer qryById(Long id) {
		Transfer transfer = transferMapper.qryById(id);
		List<TransferDetail> detailList = transferMapper.qryTransferDetailByTransferId(id);
		transfer.setTransferDetailList(detailList);
		return transfer;
	}

	/**
	 * 通过id查询详情
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public TransferDetail queryDetailById(Long id) {
		TransferDetail detail = transferMapper.queryDetailById(id);
		return detail;
	}

	public void doTransfer(Transfer transfer) {
		transfer.setStatus(3);
		transfer.setUpdateBy(WebUtil.getCurrentUser());
		transfer.setUpdateTime(new Date());
		transferMapper.updateTranser(transfer);
		if (transfer.getTransferDetailList() != null && transfer.getTransferDetailList().size() > 0) {
			for (TransferDetail detail : transfer.getTransferDetailList()) {
				detail.setStatus(1);
				detail.setUpdateBy(WebUtil.getCurrentUser());
				detail.setUpdateTime(new Date());
				transferMapper.updateTransferDetail(detail);
				
				// 更新资产表中状态为已迁移
				// ItAsset asset = new ItAsset();
				// asset.setId(detail.getAssetId());
				// asset.setStatus(6);
				// itassetMapper.updateItAsset(asset);
				
				// 更新部门信息
				AssetExtUsing assetExtUsing = new AssetExtUsing();
				assetExtUsing.setAssetId(detail.getAssetId());
				assetExtUsing.setManageBranchId(detail.getTargetManageBranchId());
				assetExtUsing.setUseBranchId(detail.getTargetUseBranchId());
				assetExtUsing.setEndUser(detail.getTargetEndUser());
				transferMapper.updateAssetExtUsingByAssetId(assetExtUsing);
				
				//插入资产生命周期表记录
				 AssetRecord re = new AssetRecord();
				 re.setId(IdWorker.getId());
				 re.setAssetId(detail.getAssetId());
				 re.setType(7);
				 re.setCreateBy(WebUtil.getCurrentUser());
				 re.setCreateTime(new Date());
				 assetRecordMapper.create(re);
			}
		}
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<ItAssetVo> querAssetListByParam(Page<ItAssetVo> page,Map<String, Object> params) {
		page.setRecords(transferMapper.querAssetListByParam(page,params));
		return page;
	}

}
